package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.security.auth.RefreshFailedException;
import javax.security.auth.Refreshable;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorListener;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import travel.Travel_Information_DAO;
import travel.Travel_Information_VO;
import travel.region.Region_DAO;
import travel.region.Region_service;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

public class GiyucEvent extends JDialog {

	private JPanel contentPane;
	private JTable table;
	DefaultTableModel model = null;
	static public String title;
	Travel_Information_VO tiv = new Travel_Information_VO();
	
	/**
	 * Create the frame.
	 */
	public GiyucEvent( JFrame p,  final String title ) {
		super(p);
		this.title = title;
		
		 //지역이름 가져오기
		setTitle(title);
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setModal(false);
		setBounds(100, 100, 800, 600);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setBackground(Color.black);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(22, 175, 739, 358);
		contentPane.add(scrollPane);
		
		

		
//		
//		model = new DefaultTableModel(){
//			@Override
//			public boolean isCellEditable(int row, int column) {
//				return super.isCellEditable(row, column); 
////				return false; // 수정불가
//			}
//		};
//		
//		model.addRow(rs.regionInfo());
//		
		
		
		//Jtable 정보
		
		DefaultTableModel model = new Region_service().regionInfo();
		
		
		table = new JTable(); 
		table.setModel(model);
		
		DefaultTableCellRenderer cell = new DefaultTableCellRenderer();
		cell.setHorizontalAlignment(JLabel.CENTER); 
		for(int i = 0; i <= 6 ; ++i){
			table.getColumnModel().getColumn( i ).setCellRenderer(cell);
			}
		
//		ActionListener btAt = new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				JTable tableAction = (JTable)e.getSource();
//				tiv.setTravel_votes(tableAction.getSelectedRowCount());
//				System.out.println(tiv.getTravel_votes());
//			}
		
		
//		};//눌렀을때 액션
		Action ntAct = new Action() {
			public void actionPerformed(ActionEvent e) {
				Travel_Information_VO tiv = new Travel_Information_VO();
				Travel_Information_DAO tid = new Travel_Information_DAO();
				Region_DAO rd = new Region_DAO();
				// place 넘기기 위해 불르고
				Region_service rs = new Region_service();
				
				JTable table = (JTable)e.getSource();
				
				int row = table.getSelectedRow();
				String place = (String)table.getValueAt(row, 0);//지역명
				Integer vote = (Integer)table.getValueAt(row, 5);//추천수
				tiv.setTravel_place(place);
				tiv.setTravel_votes(vote+1);
				
				rd.votesUpdate(tiv);
				JFrame p = null ;
				JOptionPane.showMessageDialog(null, "추천되었습니다");
				dispose();
				GiyucEvent eventUp = new GiyucEvent( p, title );
				eventUp.show();
			}
			@Override
			public void setEnabled(boolean b) {
			}
			@Override
			public void removePropertyChangeListener(PropertyChangeListener listener) {
			}
			@Override
			public void putValue(String key, Object value) {
				
			}
			@Override
			public boolean isEnabled() {
				return false;
			}
			@Override
			public Object getValue(String key) {
				return null;
			}
			@Override
			public void addPropertyChangeListener(PropertyChangeListener listener) {
			}
		};
		// Jtable에 버튼넣기
		new ButtonColumn(table, ntAct, 7);
		scrollPane.setViewportView(table);
		/////////////////////////////////////////////////////////////////검색 기능 수정 사항.
		final GiyucUp gu = new GiyucUp(title);
		JButton btnNewButton_1 = new JButton(title+"는?");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gu.show();
			}
		});
		btnNewButton_1.setBounds(552, 144, 100, 23);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(GiyucEvent.class.getResource("/image/"+title+".jpg")));
		lblNewLabel_1.setBounds(22, 33, 739, 101);
		contentPane.add(lblNewLabel_1);
		////////////////////////////////////////이미지넣장
		
		final ArrayList list = new ArrayList();
		JButton btnNewButton_2 = new JButton("찜하기");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Boolean> check = new ArrayList<>();
				Boolean val = false;
				System.out.println(table.getRowCount());
				for(int id = 0; id < table.getRowCount(); id++){
					
					 check.add((Boolean) table.getValueAt(id,  6));
				if(check.get(id) == true){
					for(int i = 0; i<=5; i++){
					list.add(table.getValueAt(id , i));
					System.out.println(list.get(i));
					}
					list.clear();
				} 
				}
				}//액션
				
					
			
		});
		btnNewButton_2.setBounds(664, 143, 97, 23);
		contentPane.add(btnNewButton_2);
		
	}
}